package rock.registry.zk;

import lombok.extern.slf4j.Slf4j;
import org.apache.curator.framework.CuratorFramework;
import rock.common.constants.RpcConstants;
import rock.registry.ServiceRegistry;
import rock.registry.zk.util.ZkCuratorUtil;

import java.net.InetSocketAddress;

@Slf4j
public class ZkServiceRegistryImpl implements ServiceRegistry {
    /**
     * 注册服务: 将serviceName 添加到zk指定目录下 create【Persist】Node
     * @param serviceName 服务名称
     * @param inetSocketAddress 服务地址
     */
    public void register(String serviceName, InetSocketAddress inetSocketAddress) {
        String servicePath = RpcConstants.ZK_REGISTER_ROOT_PATH + "/" + serviceName + inetSocketAddress.toString();
        CuratorFramework zkClient = ZkCuratorUtil.getZkClient();
        ZkCuratorUtil.createPersistNode(zkClient, servicePath);
    }
}
